package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.Bank;
import com.yantong.pojo.MemberCard;
import com.yantong.mapper.BankMapper;
import com.yantong.mapper.CardMapper;
import com.yantong.service.CardService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author kanghaixin
 * @date 2018/10/22.17:37
 */
@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private BankMapper bankMapper;

    @Override
    public int saveCard(MemberCard card) {
        if (card == null) {
            return 0;
        }
        return cardMapper.insertSelective(card);
    }

    @Override
    public int updateCard(MemberCard card) {
        if (card == null || card.getCdId() == null) {
            return 0;
        }
        return cardMapper.updateCard(card);
    }

    @Override
    public MemberCard getByKey(Long cdId) {
        if (cdId == null) {
            return null;
        }
        return cardMapper.getByKey(cdId);
    }

    @Override
    public PageUtils<Bank> listBank(String name, int offset, int limit) {
        PageUtils<Bank> pageUtils = new PageUtils<>();
        int total = bankMapper.countBank(name);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(bankMapper.listBanks(name, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public PageUtils<MemberCard> listMemberCards(String queryItem, Long centreId, String mobile, int offset, int limit) {
        Map<String,Object> params = new HashMap<>(4);
        if (StringUtils.isNotEmpty(queryItem)) {
            params.put("queryItem", queryItem);
        }
        if (centreId != null) {
            params.put("centreId", centreId);
        }
        if (StringUtils.isNotEmpty(mobile)) {
            params.put("mobile", mobile);
        }
        PageUtils<MemberCard> pageUtils = new PageUtils<>();
        int total = cardMapper.countByParams(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(cardMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }

    @Override
    public PageUtils<MemberCard> getMemberCards(Long mid, int offset, int limit) {
        if (mid == null) {
            return null;
        }
        Map<String,Object> params = new HashMap<>(4);
        params.put("mid", mid);

        PageUtils<MemberCard> pageUtils = new PageUtils<>();
        int total = cardMapper.countByParams(params);
        if (total >0) {
            pageUtils.setTotal(total);
            pageUtils.setRows(cardMapper.listByParams(params, new RowBounds(offset, limit)));
        }
        return pageUtils;
    }
}
